package com.hellogood.yaohuo.server.mgr;

import java.util.List;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.Quyu;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class QuyuMgr {
	public List<Quyu> getAllQuyu() {
		return DAOFactory.getInstance().createQuyuDAO().getAllQy();
	}

	public String addNewQuyu(Quyu qy) {
		Quyu temp = DAOFactory.getInstance().createQuyuDAO().getQuyuByCode(qy.getQyCode());
		if (temp == null) {
			String result = DAOFactory.getInstance().createQuyuDAO().addNewQy(qy);
			if (result.equals("")) {
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setLogId(logID);
				log.setStaffCode(MainFrame.currentUser.getStaffCode());
				log.setStaffName(MainFrame.currentUser.getStaffName());
				log.setLogDate(DateUtil.currentTime());
				log.setContent("新建区域：" + qy.getQyCode() + "-" + qy.getQyName());
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
			return result;
		} else {
			return "编码：" + qy.getQyCode() + " 已经存在!";
		}
	}

	public String deleteQuyu(String qyCode) {
		if (MgrFactory.getInstance().createQuyuOrgMgr().doesQuyuHasOrg(qyCode)) {
			return "该区域下含有组织，不能删除";
		} else {
			String result = DAOFactory.getInstance().createQuyuDAO().removeQy(qyCode);
			if (result.equals("")) {
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setLogId(logID);
				log.setStaffCode(MainFrame.currentUser.getStaffCode());
				log.setStaffName(MainFrame.currentUser.getStaffName());
				log.setLogDate(DateUtil.currentTime());
				log.setContent("删除区域：" + qyCode);
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
			return result;
		}
	}
}
